package com.mediacloud.app.newsmodule.activity.military_encyclopedia;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.fragment.baoliao.OnDeleteListener;
import com.mediacloud.app.newsmodule.fragment.baoliao.model.LocalMediaWithState;
import com.mediacloud.app.newsmodule.fragment.baoliao.model.add.SubmitImageReturnData;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Callback;



public class ErrorCorrectingAdapter extends RecyclerView.Adapter<ErrorCorrectingAdapter.ErrorCorrecting> implements View.OnClickListener {
    public List<LocalMediaWithState> selectList;
    private Context context;
    private OnDeleteListener listener;

    public ErrorCorrectingAdapter(Context context, OnDeleteListener listener) {
        this.context = context;
        this.listener = listener;
        this.selectList = new ArrayList<>();
    }

    public List<LocalMediaWithState> getSelectList() {
        return selectList;
    }

    public void setSelectList(List<LocalMediaWithState> selectList) {
        this.selectList.clear();
        this.selectList.addAll(selectList);
        if (selectList.size() < 9){
            LocalMediaWithState localMediaWithState = new LocalMediaWithState();
            localMediaWithState.setImgBackgroundRes(R.drawable.icon_wiki_pic_add);
            this.selectList.add(localMediaWithState);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ErrorCorrecting onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        return new ErrorCorrecting(LayoutInflater.from(context).inflate(R.layout.error_correct_media_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ErrorCorrecting holder, int position) {
        LocalMedia media = selectList.get(position).getLocalMedia();
        if (media != null){
            if (media.isCompressed()) {
                Glide.with(context).load(media.getCompressPath()).into(holder.image);
            } else {
                Glide.with(context).load(media.getPath()).into(holder.image);
            }
            holder.delete.setTag(position);
            holder.delete.setOnClickListener(this);
            holder.itemView.setOnClickListener(null);
            holder.delete.setVisibility(View.VISIBLE);
            int progress = selectList.get(position).getProgress();
            if(progress >0){
                holder.progressBar.setVisibility(View.VISIBLE);
                holder.progressBar.setProgress(progress);
            }else {
                holder.progressBar.setVisibility(View.GONE);
                holder.progressBar.setProgress(0);
            }
        }else{
            holder.itemView.setTag(null);
            holder.itemView.setOnClickListener(this);
            holder.delete.setVisibility(View.GONE);
            holder.progressBar.setVisibility(View.GONE);
            holder.image.setImageResource(selectList.get(position).getImgBackgroundRes());
        }

    }


    @Override
    public int getItemCount() {
        return selectList.size();
    }

    @Override
    public void onClick(View v) {
        if (v.getTag() != null){
            int position = (int) v.getTag();
            LocalMediaWithState localMediaWithState = selectList.remove(position);
            listener.onDelete(localMediaWithState);
        }else{
            listener.onClickItem();
        }

    }


    class ErrorCorrecting extends RecyclerView.ViewHolder {

        ImageView image,  delete;
        ProgressBar progressBar;

        public ErrorCorrecting(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.image);
            delete = itemView.findViewById(R.id.delete);
            progressBar = itemView.findViewById(R.id.progress_bar_h);
        }
    }

    interface OnDeleteListener {
        void onDelete(LocalMediaWithState deleteItem);
        void onClickItem();
    }
}